#!/usr/bin/env python
# -*- coding:utf-8 -*- 
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:

"""
@version: 1.0
@author: readerror
@contact: readerror@readerror.cn
@contact1: readerror000@gmail.com
@site: http://www.readerror.cn
@software: PyCharm
@file: user.py
@time: 2017/3/22 10:02
"""


class UserApiMixin(object):
    def api_call(self, api, data, async=False, period=60.0):
        NotImplementedError()

    def get_api_result(self, res, api_name=False):
        NotImplementedError()

    def log_in_by_account(self, data, async=False):
        """
        LogInByAccount
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'accountName': 'admin',
         'password': 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86',
         'session': {}}
        """
        rsp = self.api_call("org.zstack.header.identity.APILogInByAccountMsg", data, async=async)
        return self.get_api_result(rsp)

    def log_out(self, data, async=False):
        """
        LogOut
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'uuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'},
         'sessionUuid': 'ee5aa4ad6a9e4bfbb5827636e04a759d'}
        """
        rsp = self.api_call("org.zstack.header.identity.APILogOutMsg", data, async=async)
        return self.get_api_result(rsp)

    def query_account(self, data=None, async=False):
        """
        QueryAccount
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'conditions': [{"name": "name", "value": "test", "op": "="}]}
        """
        if data is None:
            data = {}
        if data.get('conditions') is None:
            data['conditions'] = []
        rsp = self.api_call('org.zstack.header.identity.APIQueryAccountMsg', data, async=async)
        return self.get_api_result(rsp)

    def create_account(self, data, async=False):
        """
        CreateAccount
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'name': 'test11111111111',
         'password': 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86',
         'session': {'uuid': 'b04de189f7954bffa77cde0a4bf0cbd8'}}
        """
        rsp = self.api_call("org.zstack.header.identity.APICreateAccountMsg", data, async=async)
        return self.get_api_result(rsp)

    def delete_account(self, data, async=False):
        """
        DeleteAccount
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'session': {'uuid': 'b04de189f7954bffa77cde0a4bf0cbd8'},
         'uuid': 'a957cd060dfa46daa1d6039df7c61071'}
        """
        rsp = self.api_call("org.zstack.header.identity.APIDeleteAccountMsg", data, async=async)
        return self.get_api_result(rsp)

    def update_account(self, data, async=False):
        """
        UpdateAccount
        :param async: Can be used to asynchronously execute code.
        :param data:
        {'password': 'b109f3bbbc244eb82441917ed06d618b9008dd09b3befd1b5e07394c706a8bb980b1d7785e5976ec049b46df5f1326af5a2ea6d103fd07c95385ffab0cacbc86',
         'session': {'uuid': 'b04de189f7954bffa77cde0a4bf0cbd8'},
         'uuid': 'dda215b702c04c6198d8db692cd310b4'}
        """
        rsp = self.api_call("org.zstack.header.identity.APIUpdateAccountMsg", data, async=async)
        return self.get_api_result(rsp)
